<?php
	if($_COOKIE['user_id']!='admin')
		Header("location:main.php");		
	require_once('start_session.php');
	//setcookie('user_id', 'famous');
	require_once('header.php');
	require_once('db_connect.php');
	require_once('global_vars.php');
	//require_once('search_bar.php');
	require_once('admin_menu.php');	
?>
<link href="./css/tables.css" rel="stylesheet" type="text/css">
<div id="user_info">
<h3>접속자 정보</h3>
<table border="0" cellspacing="0" cellpadding="0">
<thead>
	<tr>
		<th scope="col" width="4%">아이디</th>
		<th scope="col" width="4%">이름</th>
		<th scope="col" width="6%">접속IP</th>
		<th scope="col" width="3%">성별</th>
		<th scope="col" width="3%">나이</th>
		<th scope="col" width="5%">학력</th>
		<th scope="col" width="8%">전화번호</th>
		<th scope="col" width="6%">생년월일</th>
		<th scope="col" width="7%">이메일주소</th>
		<th scope="col" width="10%">주소</th>
		<th scope="col" width="5%">참여횟수</th>
		<th scope="col" width="5%">작성횟수</th>	
		<th scope="col" width="5%">카테고리</th>	
		<th scope="col" width="7%">키워드</th>	
	</tr>
</thead>
<tbody>
	<?	
	
	
	$sql="SELECT * from session";
	$res=mysqli_query($conn,$sql);
	$len=mysqli_num_rows($res);
	if($len < 1)
	{	
		echo "<tr><td colspan=13> 접속자가 없습니다 </td></tr></tbody></table></div><div id=\"footer\"></div></div></body></html>";		
		return;
	}	
	$guest_count=0;
	while($r=mysqli_fetch_array($res))
	{
		if($r[id]=='')
		{	
			$guest_count++;
			echo "<tr> <td>guest</td> ";
			echo "<td>손님($guest_count)</td>";
			echo "<td>".$r[ip]."</td></tr>";
		}
		else
		{
			$sql="SELECT * FROM member WHERE member.id='$r[id]'";
			$result=mysqli_query($conn,$sql);		
			$row=mysqli_fetch_array($result);
			
				echo "<tr>";
				echo "<td>".$row[id]."</td>";
				echo "<td>".$row[name]."</td>";
				echo "<td>".$r[ip]."</td>";
				if($row[gender]==1)
					echo "<td>"."남"."</td>";
				if($row[gender]==2)
					echo "<td>"."여"."</td>";
				echo "<td>".$row[age]."</td>";
				switch($row[edulevel])
				{
					case 1:	echo "<td>"."중졸 이하"."</td>";
					break;
					case 2:	echo "<td>"."고졸"."</td>";
					break;
					case 3:	echo "<td>"."초대졸"."</td>";
					break;
					case 4:	echo "<td>"."대졸"."</td>";
					break;
					case 5:	echo "<td>"."대학원졸 이상"."</td>";
					break;
				}			
				echo "<td>".$row[phone]."</td>";
				echo "<td>".$row[birth]."</td>";
				echo "<td>".$row[email]."</td>";
				echo "<td>".$row[addr]."</td>";
				echo "<td>".$row[answer_count]."</td>";
				echo "<td>".$row[make_count]."</td>";
				echo "<td>";				
				$sql="SELECT * FROM category_mem WHERE mem_id ='$row[id]'";
				$result2=mysqli_query($conn,$sql);				
				$length = mysqli_num_rows($result2);
				$i=1;
				while(1)
				{
					$row_category=mysqli_fetch_array($result2);
					if($i < $length)
						echo $row_category[category].",";
					else
					{
						echo $row_category[category];
						break;
					}
					$i++;	
					
				}
				echo "</td>";
				
				$sql="SELECT * FROM keyword_mem WHERE mem_id ='$row[id]'";
				$result2=mysqli_query($conn,$sql);
				echo "<td>";
				$length = mysqli_num_rows($result2);
				$i=1;
				while(1)
				{
					$row=mysqli_fetch_array($result2);
					if($i < $length)
						echo $row[keyword].",";
					else
					{
						echo $row[keyword];
						break;
					}
					$i++;	
					
				}
				echo "</td>";
				echo "</tr>";
				
			
		}		
	}	
?>
</tbody>
</table>
</div>
<div id="footer">
</div>
</div>
</body>
</html>